package com.project.config;

import com.project.shiro.UserRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public DefaultWebSecurityManager getSecurityManager(UserRealm realm) {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(realm);
        return manager;
    }
    @Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean getFactoryBean(DefaultWebSecurityManager manager){
        ShiroFilterFactoryBean factoryBean=new ShiroFilterFactoryBean();
        //设置安全管理器
        factoryBean.setSecurityManager(manager);


        //定义拦截器链
        Map<String,String> filterMap=new LinkedHashMap<>();
        //添加公共资源
      filterMap.put("/user/login","anon");
        //添加受限资源
       filterMap.put("/login","authc");
        //设置拦截器链
        factoryBean.setFilterChainDefinitionMap(filterMap);
        return factoryBean;
    }
}
